﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using DG.Tweening;

public class UIManager : MonoBehaviour
{
    public GameObject normal;
    public GameObject start;
    public GameObject pause;
    public GameObject end;
    public PopContent popContent;

    public Text endText;

    private GameManager gameManager;
    // Start is called before the first frame update
    void Start()
    {
        Time.timeScale = 1;
        start.SetActive(true);
        normal.SetActive(false);
        pause.SetActive(false);
        end.SetActive(false);
        popContent.gameObject.SetActive(false);

        gameManager = GetComponent<GameManager>();
        //gameManager.player.GetComponent<PlayerController>().enabled = false;
    }

    public void Exit()
    {
        Application.Quit();
    }

    public void StartGame()
    {
        start.SetActive(false);
        normal.SetActive(true);
        pause.SetActive(false);
        end.SetActive(false);

        gameManager.player.GetComponent<PlayerController>().enabled = true;
    }

    public void Restart()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

    public void Pause()
    {
        start.SetActive(false);
        normal.SetActive(false);
        pause.SetActive(true);
        end.SetActive(false);

        //pause.GetComponent<Image>().DOFade(1f, 0.5f).onComplete = () => { Time.timeScale = 0; };
        PanelEffect(pause, 1f,1f, 50f, () => { Time.timeScale = 0; });
    }

    public void EndGame(string result)
    {
        endText.text = "游戏" + result;

        start.SetActive(false);
        normal.SetActive(false);
        pause.SetActive(false);
        end.SetActive(true);

        PanelEffect(end, 1f, 1f, -50f);
    }

    public void EndPause()
    {
        Time.timeScale = 1;

        start.SetActive(false);
        normal.SetActive(true);
        end.SetActive(false);

        PanelEffect(pause, 0,0, -50f, () => { pause.SetActive(false); });
    }

    public void Pop(string text)
    {
        popContent.gameObject.SetActive(true);
        popContent.SetContent(text);
    }

    public void Pop(string text , float duration)
    {
        popContent.gameObject.SetActive(true);
        popContent.SetContent(text);
        Invoke("ExitPop", duration);
    }

    public void ExitPop()
    {
        popContent.Exit();
        Timer.Register(0.5f, () => {
            popContent.gameObject.SetActive(false);
        });
    }

    void PanelEffect(GameObject panel, float bgEndValue,float buttonsEndValue, float buttonsMoveDuration, TweenCallback callback = null)
    {
        Image[] images = panel.GetComponentsInChildren<Image>();
        for (int i = 0; i < images.Length; i++)
        {
            if (i == 0)
            {
                Tweener t = images[i].DOFade(bgEndValue, 0.5f);
                t.SetUpdate(true);
                continue;
            }
            else if (i == images.Length - 1)
            {
                images[i].DOFade(buttonsEndValue, 0.5f);
                images[i].gameObject.GetComponent<RectTransform>().DOBlendableLocalMoveBy(new Vector3(0, buttonsMoveDuration, 0), 0.2f + (i * 0.1f)).onComplete = callback;
            }
            else
            {
                images[i].DOFade(buttonsEndValue, 0.5f);
                images[i].gameObject.GetComponent<RectTransform>().DOBlendableLocalMoveBy(new Vector3(0, buttonsMoveDuration, 0), 0.2f + (i * 0.1f));
            }
        }
    }
}
